{extend name="layout/default" /}
{block name="title"}文件新增{/block}
{block name="content"}
<form class="layui-form" action="">
    <div class="mainBox" style="background-color: white;">
        <div class="main-container">
            <div class="main-container">
                <fieldset class="layui-elem-field layui-field-title">
                    <legend>选择文件并上传</legend>
                </fieldset>
                <div class="layui-upload">
                    <button type="button" class="pear-btn pear-btn-primary" id="upload">选择文件</button>
                    <button type="button" class="pear-btn pear-btn-success" id="uploadPost">开始上传</button>
                </div>
            </div>
        </div>
    </div>
</form>
{/block}
{block name="script"}
<script>
    layui.use(['form','jquery', 'popup', 'upload', 'button'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        let popup = layui.popup;
        let upload = layui.upload;
        let button= layui.button;
        let MODULE_PATH = "/{:request()->controller()}";

        // 上传文件
        let upload_btn_load;
        upload.render({
            elem: '#upload'
            ,url: MODULE_PATH + '/add'
            ,auto: false
            ,accept: "file"
            ,bindAction: '#uploadPost'
            ,before: function () {
                upload_btn_load = button.load({
                    elem:'#uploadPost',
                })
            }
            ,done: function(res){
                upload_btn_load.stop();
                if(Number(res.code) === 0) {
                    popup.success(res.msg, function() {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                        parent.layui.table.reload("dataTable");
                    });
                } else {
                    popup.failure(res.msg);
                }
            }
            ,error: function () {
                upload_btn_load.stop();
            }
        });

        let tp_debug = $("#think_page_trace_open");
        (tp_debug.length > 0) && tp_debug.remove();
    })
</script>
{/block}